package com.hos.controller;

import com.hos.framework.Result;
import com.hos.framework.ResultBean;
import com.hos.framework.ResultUtil;
import com.hos.pojo.Card001;
import com.hos.pojo.Patient;
import com.hos.service.Card001Service;
import com.hos.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/card001")
public class Card001Controller {

    @Autowired
    Card001Service service;
    @PostMapping("/update")
    public Result<String> update(@RequestBody Card001 card001) {
        if(card001.getId()!=null){
            System.out.println(card001.toString());
            service.updateCard001(card001);
        }else{
            return ResultUtil.fail();
        }
        return ResultUtil.ok();
    }
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Integer[] ids) {
        service.delete(ids);
        return ResultUtil.ok();
    }

    @GetMapping("/list")
    public ResultBean<List<Patient>> getPageResult(
            @RequestParam(required = false) String workerId,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Date submitDay,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        Map<String, Object> param = new HashMap<>(16);

        // 计算起始行号
        int offset = (page - 1) * limit;
        int rows = limit;

        param.put("workerId",workerId);
        param.put("name",name);
        param.put("submitDay",submitDay);
        param.put("offset", offset);
        param.put("rows", rows);

        // 统计记录数
        int totalRows = service.count(param);

        // 获取当前页结果集
        List<Card001> entities = service.getPageResult(param);

        return new ResultBean(0, "查询成功", totalRows, entities);

    }


}
